package com.zg.nacos.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.common.core.dao.IDao;
import com.zg.common.core.service.PageBaseService;
import com.zg.nacos.dao.MicroNacosDao;
import com.zg.nacos.domain.entity.MicroNacos;
import com.zg.nacos.service.MicroNacosService;
import com.zg.nacos.sys.ActiveStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.zg.nacos.sys.MicroE.MICRO_IS_ACTIVE;
import static com.zg.nacos.sys.MicroE.MICRO_IS_EXIST;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/6/28 10:12
 */
@Slf4j
@Service
public class MicroNacosServiceImpl extends PageBaseService<MicroNacos, MicroNacosDao> implements MicroNacosService {


//    @Autowired
//    @Qualifier("microNacosDao")
//    @Override
//    protected void setDao(IDao<MicroNacos> dao) {
//        this.dao = dao;
//    }

    @Override
    protected LambdaQueryWrapper<MicroNacos> buildQueryWrapper(MicroNacos microNacos) {

        LambdaQueryWrapper<MicroNacos> qw = new LambdaQueryWrapper<>();
        if(microNacos!=null){
            qw.like(!StrUtil.isEmpty(microNacos.getServiceName()), MicroNacos::getServiceName, microNacos.getServiceName());
            qw.eq(!StrUtil.isEmpty(microNacos.getServiceCode()), MicroNacos::getServiceCode, microNacos.getServiceCode());
        }
        return qw;
    }

    public List<MicroNacos> listAutoPre(){
        LambdaQueryWrapper<MicroNacos> qw = new LambdaQueryWrapper<>();
        qw.eq( MicroNacos::getActiveStatus, ActiveStatusEnum.PRE.getCode());
        qw.eq(MicroNacos::getEnAuto, 1);
        return baseDao.list(qw);
    }

    public MicroNacos getByNamespaceCode(String namespace,String serviceCode){
        LambdaQueryWrapper<MicroNacos> qw = new LambdaQueryWrapper<>();
        qw.eq( MicroNacos::getNamespace, namespace);
        qw.eq(MicroNacos::getServiceCode, serviceCode);
        return  baseDao.getOne(qw);
    }

    public void start(Long id){
        MicroNacos microNacos = baseDao.getById(id);
        if(microNacos.getActiveStatus().equals(ActiveStatusEnum.ACTIVE.getCode())){
            MICRO_IS_ACTIVE.happen();
        }
        microNacos.setActiveStatus(ActiveStatusEnum.PRE.getCode());
        baseDao.updateById(microNacos);
    }

    @Override
    protected void preAdd(MicroNacos microNacos){
        MicroNacos entity = getByNamespaceCode(microNacos.getNamespace(),microNacos.getServiceCode());
        if(entity!=null){
            MICRO_IS_EXIST.happen();
        }
    }

    @Override
    protected void preRemove(List<Long> ids) {
        List<MicroNacos> list = baseDao.listByIds(ids);
        List<MicroNacos> microNacosList = list.stream().filter(t->t.getActiveStatus().equals(ActiveStatusEnum.ACTIVE.getCode())).collect(Collectors.toList());
        if(!microNacosList.isEmpty()){
            MICRO_IS_ACTIVE.happen();
        }
    }

}
